import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { Redis } from 'ioredis'
import { RedisService } from './redis.service'

//Redis connect
const redisConnect = {
  provide: 'REDIS_CLIENT',
  inject: [ConfigService],
  useFactory: (configService: ConfigService) => {
    return new Redis({
      // Redis服务器配置
      host: configService.get('REDIS_HOST', 'localhost'),
      port: +configService.get('REDIS_PORT', '6379'),
      db: +configService.get('REDIS_DB', '0')
    })
  }
}

@Module({
  imports: [ConfigModule],
  providers: [redisConnect, RedisService],
  exports: [RedisService]
})
export class RedisModule {}
